<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改商城商品')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-mallProduct-edit" th:object="${webMallProduct}">
            <input name="id" th:field="*{id}" type="hidden">
            <input type="hidden" th:field="*{skuStr}">
            <h4 class="form-header h4">基本信息</h4>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">产品名称：</label>
                <div class="col-sm-8">
                    <input name="productName" th:field="*{productName}" class="form-control" maxlength="150" type="text" required>
                    <span class="help-block m-b-none">
                        <i class="fa fa-info-circle"></i> 最多150个字
                    </span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">展示价：</label>
                <div class="col-sm-8">
                    <div class="input-group m-b">
                        <span class="input-group-addon">¥</span>
                        <input name="price" th:field="*{price}" class="form-control number" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">购买价：</label>
                <div class="col-sm-8">
                    <div class="input-group m-b">
                        <span class="input-group-addon">¥</span>
                        <input name="targetMoney" th:field="*{targetMoney}" class="form-control number" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">库存量：</label>
                <div class="col-sm-8">
                        <input name="targetMoney" th:field="*{number}" class="form-control number" type="text" required>
                </div>
            </div>
            <!--/*--><div class="form-group">
                <label class="col-sm-3 control-label">SKU：</label>
                <div class="col-sm-8">
                    <textarea name="skuStr" class="form-control">[[*{skuStr}]]</textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">运费模板：</label>
                <div class="col-sm-8">
                    <input name="postage" th:field="*{postage}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">已购买金额：</label>
                <div class="col-sm-8">
                    <input name="alreadyMoney" th:field="*{alreadyMoney}" class="form-control" type="text">
                </div>
            </div>
            <!--*/-->
            <!--<div class="form-group">
                <label class="col-sm-3 control-label">最多购买数量：</label>
                <div class="col-sm-8">
                    <input name="maxBuy" th:field="*{maxBuy}" class="form-control number" type="text">
                </div>
            </div>-->
            <!--<div class="form-group">
                <label class="col-sm-3 control-label">最多可用积分：</label>
                <div class="col-sm-8">
                    <input name="useScore" th:field="*{useScore}" class="form-control number" type="text">
                </div>
            </div>-->
            <div class="form-group">    
                <label class="col-sm-3 control-label">最少使用积分：</label>
                <div class="col-sm-8">
                    <input name="minScore" th:field="*{minScore}" class="form-control number" type="text">
                    <span class="help-block m-b-none">
                        <i class="fa fa-info-circle"></i> 此项不填或者填0，则不限制下单时最少抵扣的积分<br/>
                        注意：1积分对应[[${score_value}]]元，设置方式：系统管理-参数设置-积分价值。
                    </span>
                </div>
            </div>
            <!--<div class="form-group">
                <label class="col-sm-3 control-label">销量：</label>
                <div class="col-sm-8">
                    <input name="supportNum" th:field="*{supportNum}" class="form-control number" type="text">
                </div>
            </div>-->
            <!--/*--><div class="form-group">
                <label class="col-sm-3 control-label">产品简述：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{sketch}">
                    <div class="summernote" id="sketch"></div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">产品期限：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endDate" th:value="${#dates.format(webMallProduct.endDate, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div><!--*/-->
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">发布状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                        <input type="radio" th:id="${'releaseStatus_' + dict.dictCode}" name="releaseStatus" th:value="${dict.dictValue}" th:field="*{releaseStatus}" required>
                        <label th:for="${'releaseStatus_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                    <span class="help-block m-b-none">
                        <i class="fa fa-info-circle"></i> 必须是已发布才能到小程序
                    </span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">是否推荐：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                        <input type="radio" th:id="${'isRecommend_' + dict.dictCode}" name="isRecommend" th:value="${dict.dictValue}" th:field="*{isRecommend}">
                        <label th:for="${'isRecommend_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                    <span class="help-block m-b-none">
                        <i class="fa fa-info-circle"></i> 选择是则推送到小程序首页
                    </span>
                </div>
            </div>
            <!--/*--><div class="form-group">
                <label class="col-sm-3 control-label">是否热门：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                        <input type="radio" th:id="${'isHot_' + dict.dictCode}" name="isHot" th:value="${dict.dictValue}" th:field="*{isHot}">
                        <label th:for="${'isHot_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div><!--*/-->
            <!--/*-->
            <div class="form-group">    
                <label class="col-sm-3 control-label">字典中的分类(一级)：</label>
                <div class="col-sm-8">
                    <input name="type1" th:field="*{type1}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">字典中的分类(二级)：</label>
                <div class="col-sm-8">
                    <input name="type2" th:field="*{type2}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">字典中的分类(三级)：</label>
                <div class="col-sm-8">
                    <input name="type3" th:field="*{type3}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">成本价：</label>
                <div class="col-sm-8">
                    <input name="costPrice" th:field="*{costPrice}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品来源连接：</label>
                <div class="col-sm-8">
                    <input name="source" th:field="*{source}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品的供货商：</label>
                <div class="col-sm-8">
                    <input name="supplierName" th:field="*{supplierName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">一级分销奖金：</label>
                <div class="col-sm-8">
                    <input name="fenxiao" th:field="*{fenxiao}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">二级分销奖金：</label>
                <div class="col-sm-8">
                    <input name="fenxiao2" th:field="*{fenxiao2}" class="form-control" type="text">
                </div>
            </div><!--*/-->
            <h4 class="form-header h4">商品SKU</h4>
            <div class="form-group">
                <label class="col-sm-3 control-label">属性：</label>
                <div class="col-sm-8">
                    <div class="row">
                        <div class="col-md-2">
                            <input type="text" placeholder="比如填颜色" class="form-control" id="skuName">
                        </div>
                        <div class="col-md-8">
                            <input type="text" placeholder="参数值:价格" class="form-control" id="skuValue">
                        </div>

                        <!--<a class="btn btn-white btn-bitbucket add">
                            <i class="fa fa-plus"></i>
                        </a>
                        <a class="btn btn-white btn-bitbucket sub">
                            <i class="fa fa-times"></i>
                        </a>-->
                    </div>
                    <span class="help-block m-b-none">
                        <i class="fa fa-info-circle"></i> 比如：颜色  红色：20，绿色：30，蓝色：30
                    </span>
                </div>
            </div>

            <h4 class="form-header h4">图文信息</h4>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">展示图：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="images" th:field="*{images}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="images" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">产品详细：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{content}">
                    <div class="summernote" id="content"></div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/mallProduct";
        $("#form-mallProduct-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            var skuValue = $("#skuValue").val();
            skuValue = skuValue.replace("：",":");
            skuValue = skuValue.replace("，",",");
            var skuName = $("#skuName").val();
            var skuStr = skuName+"-"+skuValue;
            $("#skuStr").val(skuStr);
            if ($.validate.form()) {

                $.operate.saveTab(prefix + "/edit", $('#form-mallProduct-edit').serialize());
            }
        }

        $("input[name='endDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            val = val.split(",");
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: val,
                maxFileCount: 3,
                autoReplace: false
            }).on('fileuploaded', function (event, data, previewId, index) {
                var val = $("input[name='" + event.currentTarget.id + "']").val();
                if(val != ""){
                    val = val.split(",");
                    val.push(data.response.url);
                    $("input[name='" + event.currentTarget.id + "']").val(val.join(","));
                }else{
                    $("input[name='" + event.currentTarget.id + "']").val(data.response.url);
                }
            }).on('fileremoved', function (event, id, index) {
                var val = $("input[name='" + event.currentTarget.id + "']").val();
                val = val.split(",");
                if(id.indexOf("init") > 0){
                    //表示初始的图片
                    var fileIndex = id.substring(id.lastIndexOf("-")+1); // thumb-images-init-0
                    val.splice(fileIndex,1);
                }else if(id.indexOf("images") > 0){
                    //手动添加的图片
                    var fileName = id.substring(id.indexOf("_")+1);
                    for(var index = 0 ; i < val.length ; i ++){
                        if(val[index] == fileName){
                            val.splice(index,1);
                            break;
                        }
                    }
                }
                $("input[name='" + event.currentTarget.id + "']").val(val.join(","));
            })
            $(this).fileinput('_initFileActions');
        });

        $(function() {
            var skuStr = $("#skuStr").val();
            $("#skuName").val(skuStr.substring(0,skuStr.indexOf("-")));
            $("#skuValue").val(skuStr.substring(skuStr.indexOf("-")+1))

            $(".form-group").on("click",".add",function (){
                var obj = $(this);
                var row = obj.parents(".row").prop("outerHTML");
                obj.parents(".row").after(row);

            })
            $(".form-group").on("click",".sub",function (){
                var count = $(".sub").length;
                if(count == 1)return;
                var obj = $(this);
                var row = obj.parents(".row").remove();

            })

            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    height:"200px",
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });
    </script>
</body>
</html>